package net.huashitong.Indicators.web;

import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.service.RedisService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.sys.util.UserUtils;
import com.sirdc.modules.utils.NumberUtils;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Indicators.dto.BA01DTO;
import net.huashitong.Indicators.dto.StudentDTO;
import net.huashitong.Indicators.entity.AB01;
import net.huashitong.Indicators.entity.AB02;
import net.huashitong.Indicators.entity.BA01;
import net.huashitong.Indicators.entity.BA02;
import net.huashitong.Indicators.filter.BA01Filter;
import net.huashitong.Indicators.service.*;
import net.huashitong.until.Until;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/sys/course")
public class CourseCtrl extends JsonBaseController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private BA01Service ba01Service; //课程表
    @Autowired
    private AB01Service ab01Service;//学生信息

    @Autowired
    private AB02Service ab02Service;//学生信息
    @Autowired
    private BattingService service;
    private  Timer timer1;
    private Timer timer2;

    @Override
    protected String getView(String s) {
        return "modules/sys/course/"+s;
    }

    //载入页面
//    @RequestMapping(method= RequestMethod.GET)
//    public String initPage(Model model)
//    {
//        return getView("query");
//    }

    @RequestMapping(method= RequestMethod.GET)
    public String initPage(Model model)
    {
        AB01 ab01 = ab01Service.getAAb999(UserUtils.getUserId());
        model.addAttribute("aab999",ab01.getAAB999());
        return getView("query");
    }

    //初始化数据
    @ResponseBody
    @RequestMapping(method=RequestMethod.POST)
    public JqGrid query (@ModelAttribute BA01Filter filter )
    {

        List<BA01> ba01s = ba01Service.getAllCourse(filter);

        return coverJqGrid(filter, ba01s);
    }


    @RequestMapping(value = "/edit/{id}")
    public String eidtCours(Model model,@PathVariable String  id)
     {

         String userId = UserUtils.getUserId();
         //ab01Service.getById()
         //AB01 student  = ab01Service.queryStudent(null,userId);
         BA01 ba01  = ba01Service.getById(id); //获取课程表
         StudentDTO studentDTO  = new StudentDTO();
         studentDTO.setAAB998(userId);
         studentDTO.setBAB001(ba01.getBAB001());
         studentDTO.setBAB003(ba01.getBAB003());

         StudentDTO dto  = ab01Service.getBetting(studentDTO);
         dto.setBAB001(ba01.getBAB001());
         dto.setBAB002(ba01.getBAB002());
         dto.setBAB003(ba01.getBAB003());
         model.addAttribute("course",dto);

         StudentDTO stuDto = new StudentDTO();
         Paging paging = new Paging();
         paging.setCurrentPage(1);
         paging.setPageSize(28);
         stuDto.setPaging(paging);
         stuDto.setBAB001(ba01.getBAB001());
         List<StudentDTO> stulist = ab02Service.getCourseRank(stuDto);
         //model.addAttribute("rankList",stulist);
         model.addAttribute("pageCount",stuDto.getPaging().getPageCount());
         stuDto.setAAB998(userId);
         List<StudentDTO> rank  = ab02Service.getRank2(stuDto);

         model.addAttribute("stuRank",ObjectUtils.isBlank(rank)?"-":rank.get(0).getAAB101());




         return getView("form");
    }

    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Message beattingCourse(@ModelAttribute BA01DTO ba01DTO)
    {

        //检测选课已经结束了
        String code = redisService.get("resultCode");
        if("1".equals(code))
        {
            return coverMessage("500","选课已经结束");
        }
        //检测选课是否开始
       String chooseCode = redisService.get(Until.Choosestatue);
        if("0".equals(chooseCode))
        {
            return coverMessage("500","选课还未开始,请关注选课时间");
        }

        String userid =  SysUserUtils.getUserId();
        AB01 student  = ab01Service.queryStudent(null,userid);

        ba01DTO.setAAB001(student.getAAB001());
        ba01DTO.setAAB998(userid);
        ba01DTO.setBAB003(ba01DTO.getBAB003());
        AB02 betting  = ab02Service.getBetRecord(student.getAAB001(),ba01DTO.getBAB001(),userid);

        if(ObjectUtils.isNotBlank(betting))
        {
            try {
                service.updateAb02(betting,ba01DTO.getAAB996(),userid);
            } catch (Exception e) {
                return coverMessage("500","修改失败,余额不足");
            }
        }else {
            try {
                service.updateMemey(ba01DTO.getAAB996(),userid,null);
            } catch (Exception e) {
                return coverMessage("500","投注失败,余额不足");
            }
            service.saveAb02s(ba01DTO);
        }


        return  coverMessage("200");
    }


    @ResponseBody
    @RequestMapping(value = "/backCourse",method = RequestMethod.POST)
    public Message backCourse(@ModelAttribute BA01DTO ba01DTO)
    {


        String code = redisService.get("resultCode");
        if("1".equals(code))
        {
            return coverMessage("500","选课已经结束");
        }

        String userid =  SysUserUtils.getUserId();
        AB01 student  = ab01Service.queryStudent(null,userid);

        ba01DTO.setAAB001(student.getAAB001());
        ba01DTO.setAAB998(userid);
        ba01DTO.setBAB003(ba01DTO.getBAB003());
        AB02 betting  = ab02Service.getBetRecord(student.getAAB001(),ba01DTO.getBAB001(),userid);
        betting.setAAA001("2") ;  //退课状态
        service.updateAcountBack(betting,"0",userid);

        /*
        if(ObjectUtils.isNotBlank(betting))
        {
            try {
                service.updateAb02(betting,ba01DTO.getAAB996(),userid);
            } catch (Exception e) {
                return coverMessage("500","修改失败,余额不足");
            }
        }else {
            try {
                service.updateMemey(ba01DTO.getAAB996(),userid,null);
            } catch (Exception e) {
                return coverMessage("500","投注失败,余额不足");
            }
            service.saveAb02s(ba01DTO);
        }*/


        return  coverMessage("200");
    }

    @ResponseBody
    @RequestMapping(value = "/rankcourse",method = RequestMethod.POST)
    public Message rankCourse(@RequestBody StudentDTO stuDto)
    {

        String userId = UserUtils.getUserId();
        Paging paging = new Paging();
       if(stuDto.getCurrentPage()!=0)
        paging.setCurrentPage(stuDto.getCurrentPage());
        paging.setPageSize(28);
        stuDto.setPaging(paging);
        stuDto.setBAB001(stuDto.getBAB001());

        List<StudentDTO> stulist = ab02Service.getCourseRank(stuDto);
        for(StudentDTO item:stulist)
        {
            if(userId.equals(item.getAAB998()))
            {
                item.setAAB102(1);
            }
        }

        Map<String ,Object> map  = new HashMap<>();
        map.put("rankList",stulist);
        map.put("page",stuDto.getPaging());

        return coverMessage("200","successful",map);
    }


    @RequestMapping(value = "/testJobs",method = RequestMethod.GET)
    public Message rankCoursetes()
    {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

        String currenttime = Until.getCurrentDateTime();
        String time = "20181112 18:25:00";
        String time22 = "20181112 18:26:00";

        Date date1 = null,date2 =null;
        try {
            date1 =sdf.parse(time);
            date2 = sdf.parse(time22);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (ObjectUtils.isBlank(timer1))
        {
            timer1  = new Timer();
           // timer1.schedule(new ItemJobService(),date1);

            timer2  = new Timer();
          //  timer2.schedule(new ItemJobService(),date2);


        }
        else {

            try {
                timer1.cancel();
            }catch (IllegalStateException e)
            {
                System.out.println("时间已经取消");

            }finally {
                timer1= null;
                timer2= null;
            }



            timer1 = new Timer();
           // timer1.schedule(new ItemJobService(),date1);

            timer2  = new Timer();
           // timer2.schedule(new ItemJobService(),date2);

        }




        return coverMessage("200");
    }


}
